package com.bifrost.web.controller;

import com.bifrost.annotation.CurrentStore;
import com.bifrost.entity.MerchantStore;
import com.bifrost.common.model.ResultModel;
import com.bifrost.model.brand.BrandModel;
import com.bifrost.service.BrandService;
import com.bifrost.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author rangf
 * @date 2018/4/12
 */
@Api("品牌相关接口")
@RestController
@RequestMapping("/api/brand")
public class BrandController {

    @Autowired
    BrandService brandService;
    @Autowired
    StoreService storeService;
    @ApiOperation("获取当前门店所有商品品牌")
    @GetMapping("/store/brands")
    public ResultModel getStoreBrands(@CurrentStore Long storeId){
        MerchantStore store = storeService.getMerchantStore(storeId);
        List<BrandModel> brandModelList = brandService.getStoreBrands(store);
        if (CollectionUtils.isNotEmpty(brandModelList)) {
            Map<String, List<BrandModel>> brandMap = brandModelList.stream().collect(Collectors.groupingBy(BrandModel::getBrandNav));
            return ResultModel.ok().put("brands", brandMap);
        }
        return ResultModel.ok().put("brands", null);
    }

}
